<?php

namespace App\Kuafu\Mybank\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Shop\Model\Shop;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;

class MybankEntryModel extends BaseModel
{
    use HasFactory, KfSoftDeletes;

    protected $table = "mybank_entry";

    protected $guarded = [];

    protected $fillable = [
        "userId",
        "outOrderNo",
        "orderNo",
        "outMerchantId",
        "type",
        "businessLicenseImg",
        "identificationCardLife",
        "identificationCardBackImg",
        "identificationCardFrontImg",
        "merchantId",
        "merchantType",
        "merchantName",
        "contactMobile",
        "contactName",
        "principalMobile",
        "principalCertType",
        "principalCertNo",
        "principalPerson",
        "bussAuthType",
        "bussAuthNo",
        "bussAuthVld",
        "status",
        "preRegisterStatus",
        "preRegisterFailReason",
        "registerStatus",
        "registerFailReason",
        "arrangementNo",
        "withholdStatus",
        "withholdOpenAt",
        "withholdCloseAt",
        "createdAt",
        "remainBrokerage",
        "remainBillAmount",
        "updatedAt",
        "createdByUid",
        "updatedByUid",
        "isDeleted",
    ];

    const TYPE_BUSINESS = 1;
    const TYPE_KF       = 2;
    const TYPE_TEXT     = [
        self::TYPE_BUSINESS => "商家",
        self::TYPE_KF       => "夸父",
    ];

    const MERCHANT_TYPE_01   = "01";
    const MERCHANT_TYPE_02   = "02";
    const MERCHANT_TYPE_03   = "03";
    const MERCHANT_TYPE_TEXT = [
        self::MERCHANT_TYPE_01 => "自然人",
        self::MERCHANT_TYPE_02 => "个体工商户",
        self::MERCHANT_TYPE_03 => "企业商户",
    ];

    const STATUS_0    = 0;
    const STATUS_1    = 1;
    const STATUS_2    = 2;
    const STATUS_3    = 3;
    const STATUS_TEXT = [
        self::STATUS_0 => "待申请",
        self::STATUS_1 => "申请中",
        self::STATUS_2 => "激活中",
        self::STATUS_3 => "已激活",
    ];

    const WITHHOLD_STATUS_1    = 1;
    const WITHHOLD_STATUS_2    = 2;
    const WITHHOLD_STATUS_3    = 3;
    const WITHHOLD_STATUS_4    = 4;
    const WITHHOLD_STATUS_TEXT = [
        self::WITHHOLD_STATUS_1 => "未开通",
        self::WITHHOLD_STATUS_2 => "已申请，未生效",
        self::WITHHOLD_STATUS_3 => "已生效",
        self::WITHHOLD_STATUS_4 => "解约中待平台确认",
    ];

    const PRE_REGISTER_STATUS_0    = 0;
    const PRE_REGISTER_STATUS_1    = 1;
    const PRE_REGISTER_STATUS_2    = 2;
    const PRE_REGISTER_STATUS_TEXT = [
        self::PRE_REGISTER_STATUS_0 => "审核中",
        self::PRE_REGISTER_STATUS_1 => "成功",
        self::PRE_REGISTER_STATUS_2 => "失败",
    ];

    const REGISTER_STATUS_0    = 0;
    const REGISTER_STATUS_1    = 1;
    const REGISTER_STATUS_2    = 2;
    const REGISTER_STATUS_TEXT = [
        self::REGISTER_STATUS_0 => "注册中",
        self::REGISTER_STATUS_1 => "成功",
        self::REGISTER_STATUS_2 => "失败",
    ];

    /**
     * @return HasMany
     */
    public function shopBindRel(): HasMany
    {
        return $this->hasMany(MybankShopBindModel::class, "entryId", "id");
    }


    /**
     * @return HasManyThrough
     */
    public function shopBindInfoRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            Shop::class,
            MybankShopBindModel::class,
            "entryId",
            "id",
            "id",
            "shopId");
    }


    /**
     * 网商门店入驻信息
     * @param int $id
     * @return Builder|Builder[]|Collection|Model|null
     */
    public static function getEntryInfo(int $id)
    {
        return MybankEntryModel::query()
                               ->where('status', self::STATUS_3)
                               ->select(['id', 'userId', 'merchantId', 'merchantName', 'status', 'withholdStatus'])
                               ->find($id);
    }


    /**
     * 通过手机号查询商户信息
     * @param int $userId
     * @param string $userMobile
     * @return mixed
     */
    public static function getEntryIdByMobile(int $userId = 0, string $userMobile = '', int $status = 0)
    {
        return MybankEntryModel::query()
                               ->select([
                                   'id',
                                   'merchantId',
                                   'merchantName',
                                   'merchantType',
                                   'status',
                                   'withholdStatus',
                                   'registerStatus',
                                   'outMerchantId',
                                   'principalMobile',
                                   'withholdStatus'
                               ])
                               ->when($userMobile, function ($query) use ($userMobile) {
                                   $query->where('contactMobile', $userMobile);
                               })
                               ->when($userId, function ($query) use ($userId) {
                                   $query->where('userId', $userId);
                               })
                               ->when($status != 0, function ($query) use ($status) {
                                   $query->where('status', $status);
                               })
                               ->get()
                               ->toArray();
    }

    /**
     * 查询多个商户通过id
     * @param array $entryIds
     * @return Builder[]|Collection
     */
    public static function getEntryList(array $entryIds)
    {
        return MybankEntryModel::query()
                               ->whereIn('id', $entryIds)
                               ->select(['id', 'merchantId', 'merchantName'])
                               ->get();
    }

    /**
     * 查询多个商户通过merchantId
     * @param array $merchantIds
     * @return Builder[]|Collection
     */
    public static function getEntryByMerchantId(array $merchantIds)
    {
        return MybankEntryModel::query()
                               ->whereIn('merchantId', $merchantIds)
                               ->select(['id', 'merchantId', 'merchantName'])
                               ->get();
    }
}
